package cn.com.nes.site.service.crm.opportunity.impl;

import cn.com.nes.mybatis.agent.crm.entity.OpportunityEnergyType;
import cn.com.nes.mybatis.agent.crm.mapper.OpportunityEnergyTypeMapper;
import cn.com.nes.site.entity.em.DeleteEnum;
import cn.com.nes.site.service.crm.opportunity.OpportunityEnergyTypeService;
import cn.com.nes.site.service.crm.opportunity.bo.OpportunityEnergyTypeBo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class OpportunityEnergyTypeServiceImpl extends ServiceImpl<OpportunityEnergyTypeMapper, OpportunityEnergyType>
    implements OpportunityEnergyTypeService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(OpportunityEnergyTypeBo opportunityEnergyTypeBo) throws Exception {
        log.debug("save opportunityEnergyTypeBo:{}", opportunityEnergyTypeBo);
        this.baseMapper.deleteByOpportunityUuid(opportunityEnergyTypeBo);
        List<OpportunityEnergyType> list = new ArrayList<>();
        for (String energyType : opportunityEnergyTypeBo.getEnergyTypes()) {
            OpportunityEnergyType opportunityEnergyType = new OpportunityEnergyType();
            opportunityEnergyType.setEnergyType(energyType);
            opportunityEnergyType.setOpportunityUuid(opportunityEnergyTypeBo.getOpportunityUuid());
            opportunityEnergyType.setProjectUuid(opportunityEnergyTypeBo.getProjectUuid());
            opportunityEnergyType.setDeleted(DeleteEnum._0.getKey());
            opportunityEnergyType.setGmtCreate(opportunityEnergyTypeBo.getOperateDate());
            opportunityEnergyType.setGmtCreateId(opportunityEnergyTypeBo.getOperateUserId());
            opportunityEnergyType.setGmtModified(opportunityEnergyTypeBo.getOperateDate());
            opportunityEnergyType.setGmtModifiedId(opportunityEnergyTypeBo.getOperateUserId());
            list.add(opportunityEnergyType);
        }
        this.saveBatch(list);
    }

    /**
     * 查询能源类型范围
     * @param opportunityUuid 商务项目UUID
     * @return
     * @throws Exception
     */
    @Override
    public List<OpportunityEnergyType> getEnergyTypes(String opportunityUuid) throws Exception {
        return this.baseMapper.getEnergyTypesByOpportunityUuid(opportunityUuid);
    }

}




